<template>
  <div class="addPrice">
    <el-dialog
      title="加价"
      :visible.sync="visible"
      width="40%"
      modal-append-to-body
      center
      @close="onClose"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="170px"
      >
        <el-form-item
          :label="`订单金额${orderInfo && orderInfo.payAmount / 100}元，加价`"
          prop="orderAmount"
        >
          <el-input-number
            :controls="false"
            :precision="2"
            :step="1"
            :min="0.01"
            :max="999"
            v-model="ruleForm.orderAmount"
          ></el-input-number
          >元
        </el-form-item>
        <el-form-item
          :label="`师傅佣金${
            orderInfo && orderInfo.commissionAmount / 100
          }元，加价`"
          prop="commissionAmount"
        >
          <el-input-number
            :controls="false"
            :precision="2"
            :step="1"
            :min="0.01"
            :max="999"
            v-model="ruleForm.commissionAmount"
          ></el-input-number
          >元
        </el-form-item>
        <el-form-item label="加价原因" prop="orderReason">
          <el-select v-model="ruleForm.orderReason" placeholder="请选择">
            <el-option v-for="item in reasonList" :key="item.dictLabel" :label="item.dictLabel" :value="item.dictLabel"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="orderRemark">
          <el-input type="textarea" v-model="ruleForm.orderRemark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {apiDictAddPriceReason} from '@/api/order.js'
export default {
  components: {},
  props: {
    orderInfo: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    const checkOrderPrice = (rule, value, callback) => {
      if (value < this.ruleForm.commissionAmount) {
        callback(new Error("订单加价金额不得低于师傅佣金加价金额"));
      } else {
        callback();
      }
    };
    const checkWorkerPrice = (rule, value, callback) => {
      if (value > this.ruleForm.orderAmount) {
        callback(new Error("师傅佣金加价金额不得高于订单加价金额"));
      } else {
        callback();
      }
    };
    return {
      visible: false,
      ruleForm: {
        orderAmount: undefined,
        commissionAmount: undefined,
        orderReason: "",
        orderRemark: "",
      },
      rules: {
        orderAmount: [
          { required: true, message: "请填写订单加价金额", trigger: "blur" },
          { validator: checkOrderPrice, trigger: "change" },
        ],
        commissionAmount: [
          {
            required: true,
            message: "请填写师傅佣金加价金额",
            trigger: "blur",
          },
          { validator: checkWorkerPrice, trigger: "change" },
        ],
        orderReason: [
          { required: true, message: "请选择加价原因", trigger: "blur" },
        ],
        orderRemark: [
          { required: true, message: "请填写描述", trigger: "blur" },
        ],
      },
      reasonList:[]
    };
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      apiDictAddPriceReason().then((res) => {
        this.reasonList = res.data;
      });
    },
    onShow() {
      this.visible = true;
    },
    onClose() {
      this.visible = false;

      this.$refs.ruleForm.resetFields();
    },
    submit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          console.log(this.ruleForm, "加价格");
          this.$emit("addPrice",this.ruleForm);
          this.$nextTick(() => {
            this.onClose();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

